<?php
namespace app\common\controller;
use think\Loader;
use think\Controller;
use think\Db;

/**
* 后台公用基础控制器
* @author taigu
* @version 2017-9-22
*/
class ShouyBaseController extends Controller {
	
    protected function _initialize() {
        parent::_initialize();
        
       $cut_action = request()->action();
//      if(!session('info_user')) {
//      	$this->redirect('shouy/index/login');
//      }
		$this->assign("action",$cut_action);
    }
	
	//图片上传
    public function upload(){
    	include_once ('././vendor/aliyun-oss-php-sdk/autoload.php');
		$files = $_POST["value"];  
	    $tmp = base64_decode($files);
	    $fn = time()."_".rand(100000, 999999) . ".jpg";
		$fp = '././public/uploads/image/'. $fn;
		$fp1 = '././public/uploads/image/'. $fn;
	    $res=file_put_contents( $fp, $tmp);
		$ossClient = new \OSS\OssClient('LTAIaqGtJ3JDXJvv', 'Q2kTcyepAHX0VOR44OtszVoyOKE8gl', 'oss-cn-shanghai.aliyuncs.com');
		try{
			$res=$ossClient->uploadFile("long3",$fn,$fp1);
			$src='https://long3.oss-cn-shanghai.aliyuncs.com/'.$fn;
			unlink($fp1);
			return json(["code"=>200,'url'=>$src]);
		} catch(OssException $e) {
			return json(["code"=>404,'msg'=>'上传错误']);
		} 
	} 
	public function wang_upload(){
		include_once ('././vendor/aliyun-oss-php-sdk/autoload.php');
			foreach ($_FILES as $name => $file) {
				$info = getimagesize($file['tmp_name']);  
        		$ext = image_type_to_extension($info['2']);  
				$ossClient = new \OSS\OssClient('LTAIaqGtJ3JDXJvv', 'Q2kTcyepAHX0VOR44OtszVoyOKE8gl', 'oss-cn-shanghai.aliyuncs.com');
				$name=time()."_".rand(100000, 999999).$ext;
				try{
					$res=$ossClient->uploadFile("long3",$name,$file['tmp_name']);
					$result['code'] = 1;
					$result['msg'] = '上传成功！';
					$result['src']='http://long3.oss-cn-shanghai.aliyuncs.com/'.$name;
				} catch(OssException $e) {
					$result['code'] = 0;
					$result['msg'] = $file -> $e->getMessage();
				}
			}
		return json($result);
	}
	
}
